﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.Net;
using System.Net.Security;
using System.IO;

namespace BaseHelper.Http
{
     public class HttpHelperLite
    {

        public Encoding Encoder = Encoding.UTF8;
        public CookieContainer cookie = new CookieContainer();

        public string Get(string url,string referer="")
        {
            string html = "";
            try
            {
                HttpWebRequest request;
                if (url.ToLower().StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    request = (HttpWebRequest)HttpWebRequest.Create(url);
                    request.ProtocolVersion = HttpVersion.Version11;
                }
                else
                    request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.Referer = referer;
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";
                request.Accept = "application/json, text/javascript, */*; q=0.01";

                HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
                cookie.Add(response.Cookies);
                Stream responseStream = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, Encoder);
                html = streamReader.ReadToEnd();
                streamReader.Dispose();
            }
            catch { }
            return html;
        }

        public string Post(string url, string data,string referer="",string useragent="")
        {
            string html = "";
            try
            {
                HttpWebRequest request;

                if (url.ToLower().StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    request = (HttpWebRequest)HttpWebRequest.Create(url);
                    request.ProtocolVersion = HttpVersion.Version10;
                }
                else
                    request = (HttpWebRequest)HttpWebRequest.Create(url);

                request.Accept = "Accept:application/json, text/javascript, */*; q=0.01";
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                if (referer.Length > 0)
                    request.Referer = referer;

                if (useragent.Length > 0)
                    request.UserAgent = useragent;

                byte[] byteRequest = Encoder.GetBytes(data);
                request.ContentLength = byteRequest.Length;
                Stream stream = request.GetRequestStream();
                stream.Write(byteRequest, 0, byteRequest.Length);
                stream.Flush();
                stream.Close();

                HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
                cookie.Add(response.Cookies);
                Stream responseStream = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, Encoder);
                html = streamReader.ReadToEnd();
                streamReader.Close();
            }
            catch { }
            return html;

        }
        public Stream GetStream(string url)
        {
            Stream stream = null;
            try
            {

                HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create(url);
                request.Method = "GET";
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3";
                //request.ContentType = "image/webp,image/*,*/*;q=0.8";
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36";
                //request.CookieContainer = cookie;
                request.Timeout = 30000;//30 秒
                HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
                //cookie.Add(response.Cookies);
                stream = response.GetResponseStream();

            }
            catch (Exception ex)
            {
                Console.WriteLine("BaseHelper.Http.HttpHelperLite.cs->GetStream()." +ex.Message);
            }
            return stream;
        }

        private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true; //总是接受  
        }  
    }

}
